//
//  ViewController.m
//  03_UIGestureRecognizer
//
//  Created by dllo on 16/7/18.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 手势 7种
    // 轻拍 长按 轻扫 平移 捏合 旋转 屏幕边缘手势
// 轻拍 Tap
    // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    // 点击几次触发方法
    // tap.numberOfTapsRequired = 2; // 默认为一
    // 几根手指触发方法
    // tap.numberOfTouchesRequired = 2; // 默认为1
    // [self.view addGestureRecognizer:tap];
// 长按 longPress
    // UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    // [self.view addGestureRecognizer:longPress];
    // longPress.minimumPressDuration = 3; // 几秒后有效果 默认是0.5秒
    // longPress.allowableMovement = 10; // 长按失败前 最大的移动距离 默认是10像素
// 轻扫 Swipe
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)]; // 默认向右滑动
    swipe.direction = UISwipeGestureRecognizerDirectionLeft |UISwipeGestureRecognizerDirectionRight; // 可以设置多个 但多个方向所触发的行为要相同 如果多个方向行为不同 需要创建多个不同的轻扫手势 swipe.direction 是四个选项 全选即为1(3)1(2)1(1)1(0)  按位枚举 左右与上下有冲突
    // [self.view addGestureRecognizer:swipe]:
    // 轻扫练习
#if 0
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 200, 400)];
    // imageView的用户交互默认关闭要把它打开
    imageView.userInteractionEnabled = YES;
    UIImage *image = [UIImage imageNamed:@"u=202573534,3270552252&fm=21&gp=0.jpg"];
    [imageView setImage:image];
    [self.view addSubview:imageView];
    UISwipeGestureRecognizer *imageSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changePhoto:)];
    [imageView addGestureRecognizer:imageSwipe];
    
    UISwipeGestureRecognizer *imageSwipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeOnePhoto:)];
    imageSwipe1.direction = UISwipeGestureRecognizerDirectionLeft;
    [imageView addGestureRecognizer:imageSwipe1];
#endif
#if 0
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 200, 400)];
    // imageView的用户交互默认关闭要把它打开
    imageView.userInteractionEnabled = YES;
    UIImage *image = [UIImage imageNamed:@"u=202573534,3270552252&fm=21&gp=0.jpg"];
    [imageView setImage:image];
    [self.view addSubview:imageView];
    UISwipeGestureRecognizer *imageSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(change:)];
    [imageView addGestureRecognizer:imageSwipe];
    // 平移
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 200, 400)];
    // imageView的用户交互默认关闭要把它打开
    imageView.userInteractionEnabled = YES;
    UIImage *image = [UIImage imageNamed:@"u=202573534,3270552252&fm=21&gp=0.jpg"];
    [imageView setImage:image];
    [self.view addSubview:imageView];
    [imageView addGestureRecognizer:pan];
    // 捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 400)];
    // imageView的用户交互默认关闭要把它打开
    imageView.userInteractionEnabled = YES;
    UIImage *image = [UIImage imageNamed:@"u=1027282977,3390056124&fm=21&gp=0.jpg"];
    [imageView setImage:image];
    [self.view addSubview:imageView];
    [imageView addGestureRecognizer:pinch];
    // 旋转
    UIRotationGestureRecognizer *rote = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(roteAction:)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 400)];
    // imageView的用户交互默认关闭要把它打开
    imageView.userInteractionEnabled = YES;
    UIImage *image = [UIImage imageNamed:@"u=202573534,3270552252&fm=21&gp=0.jpg"];
    [imageView setImage:image];
    [self.view addSubview:imageView];
    [imageView addGestureRecognizer:rote];
#endif
    // 屏幕边缘 手势
    UIScreenEdgePanGestureRecognizer *screen = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenAction:)];
    [self.view addGestureRecognizer:screen];
    screen.edges = UIRectEdgeLeft;
}
// 轻拍
- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSLog(@"轻拍");
}
// 长按
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
    } else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"长按结束");
    }
}
// 轻扫
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {
    NSLog(@"轻扫");
}
- (void)changePhoto:(UISwipeGestureRecognizer *)swipe {
    UIImageView *imageView = (UIImageView *)swipe.view; // 强转成imageView
    imageView.image = [UIImage imageNamed:@"u=172690696,3532821463&fm=21&gp=0.jpg"];
    }
- (void)changeOnePhoto:(UISwipeGestureRecognizer *)swipe {
    UIImageView *imageView = (UIImageView *)swipe.view; // 强转成imageView
    imageView.image = [UIImage imageNamed:@"u=202573534,3270552252&fm=21&gp=0.jpg"];
}
// 平移
- (void)panAction:(UIPanGestureRecognizer *)pan {
    if (pan.state == UIGestureRecognizerStateBegan) {
        
    } else if (pan.state == UIGestureRecognizerStateChanged) {
        // 平移手势 根据其他view上移动的过程 得到最终点
        CGPoint point = [pan translationInView:pan.view];
        // 根据point使pan.view移动
        pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
        // pan.view 移动的店置空
        [pan setTranslation:CGPointZero inView:pan.view];
    
    } else if (pan.state == UIGestureRecognizerStateEnded) {
//        pan.view.transform = CGAffineTransformIdentity; // 返回原位
    }
}

- (void)change:(UISwipeGestureRecognizer *)swipe {
    UIImageView *imageView = (UIImageView *)swipe.view; // 强转成imageView
//    if (swipe.direction & UISwipeGestureRecognizerDirectionRight) {
//         imageView.image = [UIImage imageNamed:@"u=u=172690696,3532821463&fm=21&gp=0.jpg"];
//    } else if (swipe.direction & UISwipeGestureRecognizerDirectionLeft) {
//     imageView.image = [UIImage imageNamed:@"u=202573534,3270552252&fm=21&gp=0.jpg"];
//    }
    if (swipe.direction == 1) {
        imageView.image = imageView.image = [UIImage imageNamed:@"u=u=172690696,3532821463&fm=21&gp=0.jpg"];
    } else if (swipe.direction == 2) {
        imageView.image = [UIImage imageNamed:@"u=202573534,3270552252&fm=21&gp=0.jpg"];
    }
}
// 捏合
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {
    NSLog(@"%f", pinch.scale);
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    pinch.scale = 1;
}

// 旋转
- (void)roteAction:(UIRotationGestureRecognizer *)rote {
    rote.view.transform = CGAffineTransformRotate(rote.view.transform, rote.rotation);
    rote.rotation = 0; // 旋转弧度 置0
}
// 屏幕边缘
- (void)screenAction:(UIScreenEdgePanGestureRecognizer *)screen {
    NSLog(@"边缘手势");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
